<?php
/**
 * Created by 酒店类.
 * User: 尚金殿
 * Date: 2018/8/24
 * Time: 10:50
 */

namespace app\admin\controller;


use think\Db;

class HotelController extends CommonController
{
    protected $mysql = 'island_hotel';
    protected $pk = 'id';
    protected $status = 'status';
    protected $name = 'name';


    //酒店添加
    public function add(){
        if($this->request->isPost()){
            //获取提交的数据
            $data = $this->request->param();

            //判定数据
            if(!$data['island_id']){
                return show(0,'岛屿不能为空');
            }
            if(!$data['name']){
                return show(0,'酒店名称不能为空');
            }
            if(!$data['num']){
                return show(0,'房间数量不能为空');
            }
            if(!$data['acreage']){
                return show(0,'房屋面积不能为空');
            }
            if(!$data['price']){
                return show(0,'酒店价格不能为空');
            }

            //判定有么有主键
            if(!empty($data['id'])){
                $this->save($data);
            }

            //判定有没有轮播
            if(isset($data['img'])){
                $slider['img'] = $data['img'];
                unset($data['img']);
            }

            //酒店入库

            $hotelId = $this->getAdd($data);

            //判定酒店入库是否成功
            if($hotelId){
                //入库轮播信息
                $slider['hotel_id'] = $hotelId;
                $slider['status'] = 1;
                $res = Db::name('slider')->insertGetId($slider);

                if(!$res){
                    return show(0,'新增失败');
                }
                    return show(1,'新增成功');
            }
        }else{
            //获取全部岛屿
            $island = Db::name('island')->where('status','>',-1)->select();

            //变量输出
            $this->assign(['island'=>$island]);

            //加载模版
            return $this->fetch();
        }
    }

    //酒店编辑
    public function edit(){
        //获取酒店ID
        $id = $this->request->param('id');

        //获取酒店信息
        $hotel = Db::name($this->mysql)->where($this->pk,$id)->find();
        $img = Db::name('slider')->where('hotel_id',$id)->find();
        $hotel['img'] = $img['img'];

        //获取岛屿信息
        $island = Db::name('island')->where('status','>',-1)->select();

        //变量输出
        $this->assign(['hotel'=>$hotel,'island'=>$island]);

        //加载模版
        return $this->fetch();
    }

    //保存编辑
    public function save($data){
        if(!$data || !is_array($data)){
            return show(0,'传递的数据不合法');
        }

        //获取数据
        $id = $data[$this->pk];
        unset($data[$this->pk]);
        $data['update_time'] = time();

        //判定有没有轮播
        if(isset($data['img'])){
            $slider['img'] = implode(',',$data['img']);
            unset($data['img']);
        }

        //入库
        $res = Db::name($this->mysql)->where($this->pk,$id)->update($data);

        //轮播表入库
        if($res){
            Db::name('slider')->where('hotel_id',$id)->update($slider);

            return show(1,'更新成功');
        }
    }
}